import { atLeast } from "@/utils/kris_utils"

import FamilyMember from './fields/FamilyMember'
import EducationalExperience from './fields/EducationalExperience'
import WorkExperience from './fields/WorkExperience'
import CertificateList from './fields/CertificateList'
import Leader from './fields/Leader'

const formatter = (fieldCfg, value, fallback,payload = {}) => {
    const sets = payload.sets[fieldCfg.mapTo]

    if(!(sets instanceof Map))
        return fallback

    let mapValue = sets.get(value)

    if (!atLeast(mapValue,['label']))
        return fallback

    return mapValue.label
}

export const keyMap = {
    name:{
        label:'姓名',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'name'
    },
    gender:{
        label:'性别',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'gender'
    },
    phone:{
        label:'手机号',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'phone'
    },
    birthday:{
        label:'出生年月',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'birthday_at'
    },
    nation:{
        label:'民族',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'nation_id'
    },
    idPeriodOfValidity:{
        label:'身份证有效期',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'identity_card_at'
    },
    id:{
        label:'身份证',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'identiry_card'
    },
    currentAddress:{
        label:'现住址',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'present_address'
    },
    marriageStatus:{
        label:'婚姻状态',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'marital_id'
    },
    educationLevel:{
        label:'学历',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'education_id'
    },
    height:{
        label:'身高',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'height'
    },
    weight:{
        label:'体重',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'weight'
    },
    registeredResidenceType:{
        label:'户口类型',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'registered_residence'
    },
    educationType:{
        label:'学历性质',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'education_nature'
    },
    school:{
        label:'学校',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'school_id'
    },
    graduationDate:{
        label:'毕业日期',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'graduation_at'
    },
    major:{
        label:'专业',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'major'
    },
    speciality:{
        label:'特长',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'speciality'
    },
    bankAccountNumber:{
        label:'银行账户',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'bank_card'
    },
    bank:{
        label:'开户行',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'bank_id'
    },
    politicalStatus:{
        label:'政治面貌',
        disabled:false,
        formatter,
        ofTable:'user_details',
        mapTo:'politic_id'
    },
    socialSecurityNumber:{
        label:'社保号码',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'social_insurance'
    },
    emergencyContact:{
        label:'紧急联系人',
        disabled:false,
        formatter:(value) => {
            try {
                let data = JSON.parse(value)
                if(!atLeast(data,['name','phone']))
                    return ''

                return `${data['name']} ${data['relation']} ${data['phone']}`
            }catch(e){
                return ''
            }
        },
        ofTable:'user_details',
        mapTo:'emergency_contact'
    },
    domicile:{
        label:'户籍地',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'domicile_id'
    },
    homePhone:{
        label:'家庭电话',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'family_phone'
    },
    computerSkills:{
        label:'电脑技能',
        disabled:false,
        formatter:null,
        ofTable:'user_details',
        mapTo:'computer_power'
    },
    certificateList:{
        label:'证书列表',
        disabled:false,
        component:CertificateList,
        formatter:(fieldCfg,value,fallback,payload) => {
            try {
                return JSON.parse(value)
            }catch(e){
                console.log(e)
                return []
            }
        },
        ofTable:'user_details',
        mapTo:'certificate_list'
    },
    familyMembers:{
        label:'家庭成员',
        disabled:false,
        component:FamilyMember,
        formatter:(fieldCfg,value,fallback,payload) => {
            try {
                let politicalOutlookMap = payload.sets['politic_id'],
                    data = JSON.parse(value)

                if(!(politicalOutlookMap instanceof Map))
                    return data

                data.forEach(item => {
                    let politic = item['political_outlook']
                    // item['political_outlook'] = politicalOutlookMap.get(politic_id)?.label
                    item['political_outlook'] = politic?.label
                })

                return data
            }catch(e){
                console.log(e)
                return []
            }
        },
        ofTable:'user_details',
        mapTo:'family_situation'
    },
    workExperience:{
        label:'工作经历',
        disabled:false,
        component:WorkExperience,
        formatter:(fieldCfg,value,fallback,payload) => {
            try {
                let data = JSON.parse(value)
                return data
            }catch(e){
                return []
            }
        },
        ofTable:'user_details',
        mapTo:'work_experience'
    },
    educationalExperience:{
        label:'教育经历',
        disabled:false,
        component:EducationalExperience,
        formatter:(fieldCfg,value,fallback,payload) => {
            try {
                let data = JSON.parse(value)
                console.log(data)
                return data
            }catch(e){
                return []
            }
        },
        ofTable:'user_details',
        mapTo:'education_experience'
    },

    employeeNumber:{
        label:'工号',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'no'
    },
    post:{
        label:'职位',
        disabled:false,
        formatter,
        ofTable:'user_companys',
        mapTo:'position_id'
    },
    employeeType:{
        label:'身份',
        disabled:false,
        formatter,
        ofTable:'user_companys',
        mapTo:'identity'
    },
    entryDate:{
        label:'入职日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'entry_at'
    },
    department:{
        label:'部门',
        disabled:false,
        formatter:(value,department) => {
            const find = (value,department,path = []) => {

                if(!Array.isArray(department))
                    return null

                for(let index in department){

                    let item = department[index]
                    if(item.value === value)
                        return [...path,item]
                    
                    if(Array.isArray(item.children) && item.children.length > 0){
                        let res = find(value,item.children,[...path,item])
                        if(res !== null)
                            return res
                    }
                }

                return null
            }

            let path = find(value,department)
            if(!Array.isArray(path) || path.length <= 0)
                return ''

            return path.map(item => item.label).join(' / ')
        },
        ofTable:'user_companys',
        mapTo:'department_id'
    },
    status:{
        label:'在职状态',
        disabled:false,
        formatter,
        ofTable:'user_companys',
        mapTo:'job_status'
    },
    practiceBeginDate:{
        label:'实习/试用开始日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'practice_start_at'
    },
    practiceEndDate:{
        label:'实习/试用结束日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'practice_end_at'
    },
    dateOfbecomeARegularWorker:{
        label:'转正日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'regular_at'
    },
    leader:{
        label:'部门领导',
        disabled:false,
        formatter:null,
        component:Leader,
        ofTable:'user_companys',
        mapTo:'leader_id'
    },
    salaryOfPractice:{
        label:'实习/试用薪资',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'practice_salary'
    },
    regularSalary:{
        label:'转正薪资',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'regular_salary'
    },
    salary:{
        label:'薪资',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'current_salary'
    },
    laborContract:{
        label:'劳动合同',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'laborContract'
    }, 
    laborContractBeginDate:{
        label:'劳动合同开始日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'labor_contract_start'
    }, 
    laborContractEndDate:{
        label:'劳动合同截至日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'labor_contract_end'
    },
    departDate:{
        label:'离职日期',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'quit_at'
    },
    entryMethod:{
        label:'入职方式',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'entry_mode'
    },
    reasonForLeaving:{
        label:'离职原因',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'quit_reason'
    },
    entryCount:{
        label:'入职次数',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'entry_number'
    },
    apartment:{
        label:'宿舍',
        disabled:false,
        formatter:(value) => {
            return value?.name
        },
        ofTable:'user_companys',
        mapTo:'room_id'
    },
    physicalExamRecord:{
        label:'体检记录',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'physicalExamRecord'
    },
    departSalaryStatus:{
        label:'离职薪资状态',
        disabled:false,
        formatter,
        ofTable:'user_companys',
        mapTo:'quit_salary_status'
    },
    temporaryResidencePermit:{
        label:'临时电子居住证',
        disabled:false,
        formatter:null,
        ofTable:'user_companys',
        mapTo:'temporaryResidencePermit'
    },
}